package com.traffic.api.util;

import com.alipay.api.AlipayResponse;
import com.traffic.api.cache.MapCacheUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by churcy on 2017/3/25.
 */
public class AlipayUtils {

    public static String checkCardCache(String cardNo) {
        String cacheCardNo = (String) MapCacheUtils.getInstance().getCardMap().get("CARD_NO:" + cardNo);
        do {
            if (cacheCardNo == null) {
                MapCacheUtils.getInstance().getCardMap().put("CARD_NO:"+cardNo,cardNo);
                break;
            }
            cardNo = DateUtils.format2String(new Date(), "MMddHHmmss") + RandomUtil.createVerificationCode(6);
            cardNo = checkCardCache(cardNo);
        } while (false);
        return cardNo;
    }

    /**
     * 处理支付宝返回结果
     *
     * @param response
     * @return
     */
    public static Map<String, Object> checkAlipayResponse(AlipayResponse response) {
        String code;
        boolean flag = false;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> obj = new HashMap<>();
        do {
            if (!response.isSuccess()) {
                //请求支付宝接口失败
                obj = BaseResponse.setResponse(null, "40003", "支付宝接口" + response.getSubMsg());
                flag = true;
                break;
            }
            code = response.getCode();
            if (code.equals("10000")) {
                break;
            }
            if (code.equals("200")) {
                break;
            }
            if (code.equals("40004")) {
                //支付宝接口出错
                obj = BaseResponse.setResponse(null, "40006", "支付宝接口" + response.getSubMsg());
                flag = true;
                break;
            }

        } while (false);
        result.put("obj", obj);
        result.put("flag", flag);
        result.put("code", response.getCode());
        result.put("subMsg", response.getSubMsg());
        result.put("isSuccess", response.isSuccess());
        result.put("subCode", response.getSubCode());

        return result;
    }
}
